<template>
  <div style="padding: 15px;">
    <vxe-toolbar>
      <template v-slot:buttons>
        <el-button @click="findInventoryList">查询</el-button>
        <el-button type="primary" @click="funcSubmit">确定</el-button>
      </template>
    </vxe-toolbar>

    <vxe-table
      ref="xTable"
      border
      resizable
      stripe
      highlight-current-row
      :align="allAlign"
      show-footer
      highlight-hover-row
      row-id="id"
      class="mytable-footer"
      height="400"
      @checkbox-change="selectionChange"
      @checkbox-all="selectAll"
      :loading="loading"
      :data="tableData"
      :edit-config="{trigger: 'click', mode: 'row', showStatus: true}"
      sort-config.remote
      @sort-change="sortList"
    >
      <vxe-table-column type="checkbox" width="60"></vxe-table-column>
      <vxe-table-column  >
        <vxe-table-column field="warehouseSpaceName" min-width="200" title="库区"  />
        <template v-slot:header="{ column }">
          <el-select v-model="listQuery.warehouseSpaceCode" filterable placeholder="请选择库区" >
            <el-option v-for="item in warehouseSpaceList" :key="item.code" :label="item.name" :value="item.code" />
          </el-select>
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="locationName" min-width="200" title="库位"  />
        <template v-slot:header="{ column }">
          <el-select v-model="listQuery.locationCode" filterable placeholder="请选择库区" >
            <el-option v-for="item in locationList" :key="item.code" :label="item.name" :value="item.code" />
          </el-select>
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="goodsName" min-width="200" title="商品名称"  />
        <template v-slot:header="{ column }">
          <el-input v-model="listQuery.goodsName" placeholder="请输入商品名称" clearable />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="goodsBarCode" title="商品条码" min-width="200"  />
        <template v-slot:header="{ column }">
          <el-input v-model="listQuery.goodsBarCode" placeholder="请输入商品条码" clearable />
        </template>
      </vxe-table-column>

      <vxe-table-column>
        <vxe-table-column field="gmtManufacture" title="生产日期" min-width="200"  />
        <template v-slot:header="{ column }">
          <el-input v-model="listQuery.gmtManufacture" placeholder="请输入生产日期" clearable />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="gmtExpire" title="过期日期" min-width="200"  />
        <template v-slot:header="{ column }">
          <el-input v-model="listQuery.gmtExpire" placeholder="请输入过期日期" clearable />
        </template>
      </vxe-table-column>

      <vxe-table-column>
        <vxe-table-column field="gmtStock" title="存货日期" min-width="200"  />
        <template v-slot:header="{ column }">
          <el-input v-model="listQuery.gmtStock" placeholder="请输入过期日期" clearable />
        </template>
      </vxe-table-column>

      <vxe-table-column>
        <vxe-table-column field="receiveLot" title="入库批次" min-width="200"  />
        <template v-slot:header="{ column }">
          <el-input v-model="listQuery.receiveLot" placeholder="请输入过期日期" clearable />
        </template>
      </vxe-table-column>

      <vxe-table-column>
        <vxe-table-column field="status" title="库存状态" min-width="200" :formatter="formatterGoodsStatus"  />
        <template v-slot:header="{ column }">
          <el-select v-model="listQuery.status" filterable placeholder="请选择" >
            <el-option value="0" label="合格" />
            <el-option value="1" label="不合格" />
          </el-select>
        </template>
      </vxe-table-column>

      <vxe-table-column>
        <vxe-table-column field="isLock" title="锁定状态" min-width="200" :formatter="formatterIsLock" />
        <template v-slot:header="{ column }">
          <el-select v-model="listQuery.isLock" filterable placeholder="请选择" >
            <el-option value="0" label="否" />
            <el-option value="1" label="是" />
          </el-select>
        </template>
      </vxe-table-column>

    </vxe-table>
    <vxe-pager
      :loading="loading"
      :current-page.sync="listQuery.pageNum"
      :page-size.sync="listQuery.pageSize"
      :total="listQuery.total"
      :layouts="['PrevJump', 'PrevPage', 'Number', 'NextPage', 'NextJump', 'Sizes', 'FullJump', 'Total']"
      @page-change="handlePageChange"
    />
  </div>
</template>
<script>
import { getWarehouseSpace, getLocation } from "@/api/basicInfo";
import { inventoryCanUse } from "@/api/store";

export default {
  data() {
    return {
      tableData: [],
      loading: false,
      allAlign: "center",
      listQuery: {
        total: 0, //总数
        warehouseCode: "",
        warehouseSpaceCode: "",
        locationCode: "",
        goodsName: "",
        goodsStatus: "",
        goodsBarCode: "",
        pageNum: 1,
        pageSize: 10,
        sortField: "",
        sortType: "",
        receiveLot: "",
      },
      warehouseSpaceList: [],
      locationList: [],
      // 所有勾选的数据
      checkedRecordsMap: new Map(),
      oldRecordsMap: new Map(),
      resultMap:{
        addRecordsMap: new Map(),
        removeRecordsMap: new Map(),
      }

    };
  },

  created() {
  },
  methods: {
    // 初始化移位单code
    getInfo(warehouseCode, initRecords) {
      this.checkedRecordsMap = new Map();
      this.oldRecordsMap = new Map();
      this.resultMap.addRecordsMap = new Map();
      this.resultMap.removeRecordsMap = new Map();

      this.listQuery.warehouseCode = warehouseCode;
      this.findWarehouseSpaceList();
      this.findLocationList();
      this.initCheckedRecordsMap(initRecords);
    },

    // 根据仓库查询可用库存
    findInventoryList(){
      this.loading = true;
      inventoryCanUse(this.listQuery).then((response)=>{
        this.tableData = response.data.records;
        this.listQuery.total = response.data.total;
        for(let i = 0 ; i<this.tableData.length; i++){
          this.tableData[i].inventoryCode = this.tableData[i].code;
          if(this.checkedRecordsMap.has(this.tableData[i].inventoryCode)){
            this.$refs.xTable.setCheckboxRow(this.tableData[i], true);
          }
        }
        this.loading = false;
      })
    },

    findWarehouseSpaceList(){
      //const params = {'warehouseCode': this.listQuery.warehouseCode}
      getWarehouseSpace(this.listQuery.warehouseCode).then((response)=>{
        this.warehouseSpaceList = response.data;
      })
    },

    findLocationList(){
      const params = {'warehouseCode': this.listQuery.warehouseCode}
      getLocation(params).then((response)=>{
        this.locationList = response.data;
      })
    },

    // 添加移位单明细
    funcSubmit() {
      this.getDifferentMap();
      this.$emit('fatherAddItem', this.resultMap);

    },

    fucCancel() {
      this.$emit("fatherCancel");
    },

    formatterGoodsStatus({cellValue}){
      if(cellValue == 0){
        return "合格"
      }else if(cellValue == 1){
        return "不合格"
      }
    },
    formatterIsLock({cellValue}){
      if(cellValue == 0){
        return "否"
      }else if(cellValue == 1){
        return "是"
      }
    },

    // 分页handle
    handlePageChange({ currentPage, pageSize }) {
      this.listQuery.pageNum = currentPage;
      this.listQuery.pageSize = pageSize;
      this.findInventoryList();
    },
     // 排序
    sortList({ column, property, order }) {
      if (order) {
        this.listQuery.sortField = property;
        this.listQuery.sortType = order;
      } else {
        this.listQuery.sortField = "";
      }
      this.findList();
    },

    // 手动勾选事件
    selectionChange({selection, row, checked}){
      for(let record of selection){
        console.info(record);
     /*   this.checkedRecords.filter(item=>{
          if(item.code == )
        })*/
      }
      console.info(selection.code);
      this.updateCheckedRecords(checked, row);

    },
    // 全部勾选事件
    selectAll({selection, row, checked}){
      for(let record of selection){
        console.info(record);
      }
      console.info(selection.code);

      for(let record of selection){
        this.updateCheckedRecords(checked, record);
      }
    },


    updateCheckedRecords(checked, row){
      if(checked){
        if(!this.checkedRecordsMap.has(row.code)){
          this.checkedRecordsMap.set(row.code, row);
        }
      }else{
        if(this.checkedRecordsMap.has(row.code)){
          this.checkedRecordsMap.delete(row.code);
        }
      }
    },

    initCheckedRecordsMap(initRecords){
      console.info('init');
      if(initRecords != undefined && initRecords.length>0){

        for(let i=0;i<initRecords.length;i++){
          this.checkedRecordsMap.set(initRecords[i].inventoryCode, initRecords[i]);
          this.oldRecordsMap.set(initRecords[i].inventoryCode, initRecords[i]);
        }

      }
    },

    getDifferentMap(){
      console.info('diff')
      this.checkedRecordsMap.forEach((val, key)=>{
        if(!this.oldRecordsMap.has(key)){
          this.resultMap.addRecordsMap.set(key, val);
        }
      })

      this.oldRecordsMap.forEach((val, key)=>{
        if(!this.checkedRecordsMap.has(key)){
          this.resultMap.removeRecordsMap.set(key, val);
        }
      })
    },
  },


};
</script>

<style>
.vxe-table .vxe-body--column:not(.col--ellipsis),
.vxe-table .vxe-footer--column:not(.col--ellipsis),
.vxe-table .vxe-header--column:not(.col--ellipsis) {
  padding: 6px 0;
}
.vxe-toolbar {
  padding: 0 0 10px 0;
}
</style>
